package com.inspur.gs.sbssdl.sbwhgl.sbwhby.sbwhby.back.bizactions;

import com.inspur.edp.bef.api.be.IBEManagerContext;
import com.inspur.edp.bef.api.be.IBEService;
import com.inspur.edp.bef.api.be.IBusinessEntity;
import com.inspur.edp.bef.api.services.IBefSessionManager;
import com.inspur.edp.bef.spi.action.AbstractManagerAction;
import com.inspur.edp.svc.document.storage.api.DocStorageService;
import com.inspur.edp.svc.document.storage.entity.GspDocContent;
import com.inspur.edp.svc.document.storage.entity.GspDocMetadata;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;
import io.iec.edp.caf.rpc.client.RpcClassHolder;

import java.time.Duration;
import java.util.HashMap;

public class sbyjbyfkgetFjMgrAction extends AbstractManagerAction<Object> {
    private String metadataId;
    private String rootId;

    public sbyjbyfkgetFjMgrAction(IBEManagerContext managerContext, String metadataId, String rootId) {
        super(managerContext);
        this.metadataId = metadataId;
        this.rootId = rootId;
    }

    @Override
    public void execute() {
        String rootId = "maintenance";
        RpcClassHolder rpcClassHolder = SpringBeanUtils.getBean(RpcClassHolder.class);
        DocStorageService docStorageService = rpcClassHolder.getRpcClass(DocStorageService.class);
        try {
            SpringBeanUtils.getBean(IBefSessionManager.class).createSession(Duration.ofMinutes(5));
            GspDocContent docContent = docStorageService.getDocContent(metadataId, rootId);
            GspDocMetadata metadataInfo = docStorageService.getMetadataInfo(metadataId);
            byte[] fileContent = docContent.getFileContent();
            HashMap<String, Object> result = new HashMap<>();
            result.put("metadataId", metadataId);
            result.put("fileType", metadataInfo.getDocType());
            result.put("fileContent", fileContent);
            setResult(result);

        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            SpringBeanUtils.getBean(IBefSessionManager.class).closeCurrentSession();
        }
    }

    private IBusinessEntity getEntity(String dataId) {
        return (IBusinessEntity) super.getBEManagerContext().getEntity(dataId);
    }

    private IBEService getMgr() {
        return (IBEService) super.getBEManagerContext().getBEManager();
    }
}